package com.tools.project.demo;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextArea;
import com.jfoenix.controls.JFXTextField;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.StringConverter;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * Created by 2025/5/26
 *
 * @author ldh
 * @version V1.0
 * @description: 待补充...
 */
public class UrlConvert extends Application {

    @Override
    public void start(Stage stage) throws Exception {

        AnchorPane root = new AnchorPane();
        ObservableList<Node> children = root.getChildren();
        root.setStyle("-fx-background-color:WHITE");

        JFXTextArea url = new JFXTextArea();
        url.setLabelFloat(true);
        url.setPromptText("需要转换的链接");
        url.setMinWidth(600);
        url.setLayoutX(50);
        url.setLayoutY(150);
        url.setMinHeight(200);
        url.setMaxHeight(200);
        url.setFont(new Font(20));
        url.setWrapText(true);
        children.add(url);

        JFXTextArea convertResult = new JFXTextArea();
        convertResult.setLabelFloat(true);
        convertResult.setPromptText("执行结果");
        convertResult.setMinWidth(600);
        convertResult.setWrapText(true);
        convertResult.setFont(new Font(20));
        convertResult.setLayoutX(50);
        convertResult.setLayoutY(400);
        convertResult.setMinHeight(200);
        convertResult.setMaxHeight(200);
        children.add(convertResult);


        JFXTextField versionField = new JFXTextField();
        versionField.setPromptText("当前版本号");
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        // 格式化日期
        String formattedDate = currentDate.format(formatter);
        versionField.setText(formattedDate);
        versionField.setLayoutX(50);
        versionField.setLayoutY(700);
        versionField.setPrefHeight(40);
        versionField.setMinWidth(120);
        versionField.setMaxWidth(120);
        versionField.setPrefWidth(120);
        versionField.setStyle("-fx-text-fill: red; -fx-font-size: 20px; -fx-background-color: #F0F0F0;");
        children.add(versionField);


        ComboBox<Label> env = new ComboBox<>();
        ObservableList<Label> items = env.getItems();

        items.add(new Label("dev"));
        items.add(new Label("fat"));
        items.add(new Label("pre"));
        items.add(new Label("pre_temp"));
        items.add(new Label("uat"));
        items.add(new Label("prod"));
        env.setStyle(" -fx-font-size: 20px;-fx-background-color: #F0F0F0; ");
        env.setPromptText("执行环境");
        env.setPrefHeight(40);
        env.setLayoutX(300);
        env.setLayoutY(700);
        env.setMinWidth(200);
        env.setMaxWidth(200);
        env.setConverter(new StringConverter<>() {
            @Override
            public String toString(Label obj) {
                return obj == null ? "" : obj.getText(); // 返回对象属性而非默认toString
            }

            @Override
            public Label fromString(String string) {
                return env.getItems().stream()
                        .filter(item -> item.getText().equals(string))
                        .findFirst().orElse(null);
            }
        });
        // 添加选中时的缩放动画
        env.setCellFactory(listView -> new ListCell<>() {
            @Override
            protected void updateItem(Label item, boolean empty) {
                super.updateItem(item, empty);
                if (empty || item == null) {
                    setText(null);
                } else {
                    setText(item.getText());
                    // 根据选中状态设置颜色
                    if (isSelected()) {
                        setTextFill(Color.RED);  // 选中时白色
                    } else {
                        setTextFill(Color.BLACK);  // 未选中时黑色
                    }
                }
            }
        });
        env.setEditable(true);
        TextField editor = env.getEditor();
        editor.setEditable(false);
        editor.textProperty().addListener(((observableValue, s, t1) -> {
            editor.setStyle("-fx-font-size: 20px;-fx-text-fill:red;");
        }));

        children.add(env);




        JFXButton button = new JFXButton("开始转换");
        button.setStyle("-fx-background-color: #38B0DE; -fx-text-fill:#00FFFF;-fx-font-size: 20px;-fx-font-weight: bold;");
        button.setMinWidth(120);
        button.setMaxWidth(120);
        button.setPrefWidth(120);
        button.setLayoutX(600);
        button.setLayoutY(700);
        button.setPrefHeight(30);
        children.add(button);




        JFXTextField tokenField = new JFXTextField();
        tokenField.setPromptText("执行接口的Token");
        tokenField.setLayoutX(50);
        tokenField.setLayoutY(750);
        tokenField.setPrefHeight(40);
        tokenField.setMinWidth(500);
        tokenField.setMaxWidth(500);
        tokenField.setPrefWidth(120);
        tokenField.setStyle("-fx-text-fill: red; -fx-font-size: 20px; -fx-background-color: #F0F0F0;");
        children.add(tokenField);

        JFXTextField result = new JFXTextField();
        result.setPromptText("执行接口的结果");
        result.setLayoutX(50);
        result.setLayoutY(800);
        result.setPrefHeight(80);
        result.setMinWidth(500);
        result.setMaxWidth(500);
        result.setPrefWidth(120);
        result.setStyle("-fx-text-fill: red; -fx-font-size: 20px; -fx-background-color: #F0F0F0;");
        children.add(result);

        JFXButton buttonSubmit = new JFXButton("开始执行");
        buttonSubmit.setStyle("-fx-background-color: #38B0DE; -fx-text-fill:#00FFFF;-fx-font-size: 20px;-fx-font-weight: bold;");
        buttonSubmit.setMinWidth(120);
        buttonSubmit.setMaxWidth(120);
        buttonSubmit.setPrefWidth(120);
        buttonSubmit.setLayoutX(600);
        buttonSubmit.setLayoutY(800);
        buttonSubmit.setPrefHeight(30);
        children.add(buttonSubmit);

        CheckBox checkBox = new CheckBox("是否为uat");
        checkBox.setPrefSize(150, 50);
        checkBox.setLayoutX(860);
        checkBox.setLayoutY(800);
        checkBox.setMinWidth(50);
        checkBox.setMaxWidth(200);
        children.add(checkBox);

        JFXButton buttonQuery = new JFXButton("查询结果");
        buttonQuery.setStyle("-fx-background-color: #38B0DE; -fx-text-fill:#00FFFF;-fx-font-size: 20px;-fx-font-weight: bold;");
        buttonQuery.setMinWidth(120);
        buttonQuery.setMaxWidth(120);
        buttonQuery.setPrefWidth(120);
        buttonQuery.setLayoutX(730);
        buttonQuery.setLayoutY(800);
        buttonQuery.setPrefHeight(30);
        children.add(buttonQuery);



        //转换Json
        button.addEventHandler(MouseEvent.MOUSE_PRESSED, mouseEvent -> {
            if (MouseEvent.MOUSE_PRESSED.equals(mouseEvent.getEventType())) {
                if (env.getValue() == null) {
                    convertResult.setText("请输入执行环境");
                    return;
                }
                if (url.getText() == null) {
                    convertResult.setText("请输入需要处理的链接");
                    return;
                }
                if (versionField.getText() == null) {
                    convertResult.setText("请输入版本号");
                    return;
                }

                String convert = GitLabFileFetcherUtil.convert(url.getText(), env.getValue().getText(), versionField.getText());
                convertResult.setText(convert);
                result.setText("");
            }
        });

        //接口执行
        buttonSubmit.addEventHandler(MouseEvent.MOUSE_PRESSED, mouseEvent -> {
            if (MouseEvent.MOUSE_PRESSED.equals(mouseEvent.getEventType())) {
                result.setText("");
                if (env.getValue() == null) {
                    result.setText("请输入执行环境");
                    return;
                }
                if (StringUtils.isBlank(url.getText())) {
                    result.setText("请输入需要处理的链接");
                    return;
                }

                if (StringUtils.isBlank(versionField.getText())) {
                    result.setText("请输入版本号");
                    return;
                }

                if (StringUtils.isBlank(convertResult.getText())) {
                    result.setText("请先转换JSON在执行");
                    return;
                }
                String token = tokenField.getText();
                if (StringUtils.isEmpty(token)) {
                    token = GitLabFileFetcherUtil.getToken(env.getValue().getText());
                    tokenField.setText(token);
                }
                String rst = GitLabFileFetcherUtil.submit(url.getText(), env.getValue().getText(), token, convertResult.getText(), checkBox.isSelected());
                result.setText(rst);
            }
        });

        //查询结果
        buttonQuery.addEventHandler(MouseEvent.MOUSE_PRESSED, mouseEvent -> {
            if (MouseEvent.MOUSE_PRESSED.equals(mouseEvent.getEventType())) {
                result.setText("");
                if (env.getValue() == null) {
                    result.setText("请输入执行环境");
                    return;
                }
                if (StringUtils.isBlank(url.getText())) {
                    result.setText("请输入需要处理的链接");
                    return;
                }

                if (StringUtils.isBlank(convertResult.getText())) {
                    result.setText("请先转换JSON在执行");
                    return;
                }
                String token = tokenField.getText();
                if (StringUtils.isEmpty(token)) {
                    token = GitLabFileFetcherUtil.getToken(env.getValue().getText());
                    tokenField.setText(token);
                }
                String rst = GitLabFileFetcherUtil.query(url.getText(), env.getValue().getText(), token, convertResult.getText(), checkBox.isSelected());
                result.setText(rst);
            }
        });


        // 创建一个场景
        final Scene scene = new Scene(root, 1000, 900);
        scene.getStylesheets().add(UrlConvert.class.getResource("/css/jfoenix-components.css").toExternalForm());

        stage.setTitle("配置转换");
        stage.setScene(scene);
        stage.show();

    }


    public static void main(String[] args) {
        launch(args);
    }
}
